package com.openlogic.flow.controller;

import java.util.List;

import org.dromara.warm.flow.core.utils.AssertUtil;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.annotation.Path;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.core.handle.Result;
import org.noear.solon.data.annotation.Tran;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.utils.DateUtils;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.common.security.utils.SecurityUtil;
import com.openlogic.flow.common.constants.FlowConstant;
import com.openlogic.flow.common.enums.FlowCategoryStatus;
import com.openlogic.flow.domain.entity.FlowCategory;
import com.openlogic.flow.domain.entity.table.Tables;
import com.openlogic.flow.domain.query.FlowCategoryQuery;
import com.openlogic.flow.service.IFlowCategoryService;

/**
 * @ClassName: FlowCategoryController
 * @Description: 流程类型 控制层
 * @author weiyongpeng
 * @date 2025年2月11日 下午8:13:44
 */
@Controller
@Mapping("flow/category")
public class FlowCategoryController extends BaseController {

	@Inject
	IFlowCategoryService flowCategoryService;

	/**
	 * 描述：查询流程类型树
	 * @Title: list
	 * @param flowCategoryQuery
	 * @return
	 * @author weiyongpeng
	 * @date 2025年2月11日 下午8:35:05
	 */
	@Get
	@Mapping("listCategoryTree")
	@QzLog(title = "查询流程类型信息", businessType = BusinessType.LIST)
	public List<FlowCategory> list(@Param FlowCategoryQuery flowCategoryQuery) {
		List<FlowCategory> list = flowCategoryService.selectTreeDataList(flowCategoryQuery);
		return list;
	}
	
	/**
     * 查询分类列表（排除节点）
     */
    @Get
    @Mapping("list/exclude/{id}")
    public List<FlowCategory> excludeChild(@Path Integer id) {
        return flowCategoryService.excludeChild(id);
    }
	
    /**
     * 查询部门详情
     */
    @Get
    @Mapping("{id}")
    public FlowCategory info(@Path Integer deptId) {
        return flowCategoryService.info(deptId);
    }
	/**
	 * 描述：流程类型子节点信息查询
	 * @Title: listChild
	 * @param page
	 * @param flowCategoryQuery
	 * @return
	 * @author weiyongpeng
	 * @date 2025年2月11日 下午8:44:09
	 */
	@Get
	@Mapping("listCategoryChild")
	@QzLog(title = "查询流程类型信息", businessType = BusinessType.LIST)
	public PageData<FlowCategory> listChild(Page<FlowCategory> page, @Param FlowCategoryQuery flowCategoryQuery) {
		AssertUtil.isNull(flowCategoryQuery.getParentId(), "流程类型父级id 不能为空");
		startPage(page);
		List<FlowCategory> list = flowCategoryService.selectByPid(flowCategoryQuery.getParentId());
		return getPageData(list);
	}

	/**
	 * 描述：流程类型新增
	 * @Title: add
	 * @param flowCategory
	 * @return
	 * @author weiyongpeng
	 * @date 2025年2月11日 下午8:44:24
	 */
	@Post
	@Mapping("add")
	@QzLog(title = "流程类型新增", businessType = BusinessType.INSERT)
	@Tran
	public Result<Boolean> add(@Body FlowCategory flowCategory) {
		flowCategory.setCreateBy(SecurityUtil.getUserName());
		flowCategory.setStatus(FlowCategoryStatus.ABLE.getType());
		flowCategory.setCreateTime(DateUtils.getNowDate());
		return Result.succeed(flowCategoryService.insert(flowCategory));
	}

	/**
	 * 描述：流程类型修改
	 * @Title: edit
	 * @param flowCategory
	 * @return
	 * @author weiyongpeng
	 * @date 2025年2月12日 上午11:05:44
	 */
	@Put
	@Mapping("/edit")
	@QzLog(title = "流程类型修改", businessType = BusinessType.UPDATE)
	public Result<Boolean> edit(@Body FlowCategory flowCategory) {
		flowCategory.setUpdateBy(SecurityUtil.getUserName());
		flowCategory.setUpdateTime(DateUtils.getNowDate());
		return Result.succeed(flowCategoryService.update(flowCategory));
	}

	/**
	 * 描述：流程类型删除
	 * @Title: delete
	 * @param id
	 * @return
	 * @author weiyongpeng
	 * @date 2025年2月11日 下午8:50:03
	 */
	@Delete
	@Mapping("delete/{id}")
	@QzLog(title = "流程类型删除", businessType = BusinessType.DELETE)
	public Result<Boolean> delete(@Path String id) {
		AssertUtil.isTrue(flowCategoryService.exists(QueryWrapper.create().eq(FlowCategory::getId, id)
				.and(Tables.flowCategory.type.eq(FlowConstant.FLOW_CATEGORY_ROOT))), "不能删除根节点，请重新选择!");
		return Result.succeed(flowCategoryService.delete(id));
	}
}
